﻿using log4net;
using OA.BLL;
using OA.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Text;
using OA.Core;
using OA.Web.Controllers;
using OA.Web.Attribute;
using OA.Web.Code;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Mvc;
using OA.Core.Helper;
using OA.Model.DB;
using OA.Core.Oss;

namespace OA.Web
{
    [Area("Admin")]
    [AdminFilter]
    public class BaseAdminController : BaseController
    {         
        public AdminInfo Info { get; set; }
        private readonly ICookie _cookie;
        private readonly IMemoryCache _cache;

        public string Signature { get; set; }

        public List<string> Codes { get; set; }

        public BaseAdminController(IHttpContextAccessor accessor, ICookie cookie, IMemoryCache cache) : base(accessor,cookie, cache)
        {
            _cookie = cookie;
            _cache = cache;
            string userID = _cookie.GetCookies(CookieHelper.AdminID);
            userID = userID.FromBase62<string>();
            this.Info = this.dbContextFactory.DbContext.AdminInfo.Where(m => m.UserName == userID).FirstOrDefault();
            if (Info == null)
            {
                return;
            }
            else
            {
                this.Signature = this.Info.UserName.ToBase62();
                CallContext.SetData("AdminInfo", Info);
            }           
             
        }

        public ContentResult FinshAdd()
        {
            return Content("<script>window.parent.FinshAdd();</script>", "text/html");
        }

        /// <summary>
        /// 当前页面刷新
        /// </summary>
        /// <returns></returns>
        public ContentResult FinshAdd2(string msg)
        {
            return Content($"<script>window.parent.FinshAdd('{msg}');</script>", "text/html");
        }

        public ContentResult FinshDelete()
        {
            return Content("<script>window.parent.FinshDelete();</script>", "text/html");
        }

        public ContentResult FinshEdit()
        {
            return Content("<script>window.parent.FinshEdit();</script>", "text/html");
        }

        public ContentResult FinshOther(string msg)
        {
            return Content($"<script>window.parent.FinshOther('{msg}');</script>", "text/html");
        }

        /// <summary>
        /// 隐藏弹出框，不刷新页面
        /// </summary>
        /// <returns></returns>
        public ContentResult FinshOther2()
        {
            return Content($"<script>window.parent.FinshOther2();</script>", "text/html");
        }

        public ContentResult FinshFrame(string msg)
        {
            return Content($"<script>window.parent.FinshFrame('{msg}');</script>", "text/html");
        }
        
    }
}